package org.netty.mvc.tool;

import org.netty.mvc.entity.ParamProperties;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 获取配置文件数据
 *
 * @Author Jun jie Yi
 * @Date 2022/3/7 17:07
 */
public class PropertiesUtil {
    private static InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties");
    private static Properties properties = new Properties();

    public static ParamProperties getProperty() {
        //加载当前jar包下的application.properties配置文件
        File file = new File(".", "application.properties");
        try {
            if (file.exists()) {
                inputStream = new FileInputStream(file);
            }
            properties.load(inputStream);
            properties.list(System.out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ParamProperties paramProperties = new ParamProperties();
        paramProperties.setPort(convertInteger("port"));
        paramProperties.setSslPort(convertInteger("ssl_port"));
        paramProperties.setWorkerGroup(convertInteger("workerGroup"));
        paramProperties.setBossGroup(convertInteger("bossGroup"));
        return paramProperties;
    }

    private static Integer convertInteger(String param) {
        String property = properties.getProperty(param);
        if (property != null) {
            return Integer.parseInt(property);
        }
        return null;
    }
}
